<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>添加商品</title>
    <link rel="stylesheet" type="text/css" href="layui/css/layui.css">
    <script src="layui/layui.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div class="layui-container" style="margin-top: 20px">
    <div class="layui-row">
        <div class="layui-col-md6 layui-col-md-offset3">
            <form class="layui-form"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
                <input type="hidden" id="gid" th:value="${goods.gid}">
                <div class="layui-form-item">
                    <label class="layui-form-label">商品名称</label>
                    <div class="layui-input-block">
                        <input type="text" id="gname" name="gname" placeholder="商品名称" class="layui-input" th:value="${goods.gname}">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">产地</label>
                    <div class="layui-input-block">
                        <input type="text" id="producer" name="producer" placeholder="产地" class="layui-input" th:value="${goods.producer}">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">价格</label>
                    <div class="layui-input-block">
                        <input type="text" id="price" name="price" placeholder="价格" class="layui-input" th:value="${goods.price}">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">商品类别</label>
                    <div class="layui-input-block">
                        <select id="categorylevelone" th:name="categorylevelone" th:switch="${goodsCategoryList}">
                            <option value="">请选择类别</option>
                            <option th:selected="${goodsCategory.name == '水果'}" th:value="水果">水果</option>
                            <option th:selected="${goodsCategory.name == '香蕉'}" th:value="香蕉">香蕉</option>
                            <option th:selected="${goodsCategory.name == '精品香蕉'}" th:value="精品香蕉">精品香蕉</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">请填写描述</label>
                    <div class="layui-input-block">
                        <textarea id="lable" name="lable" placeholder="请输入内容" class="layui-textarea" th:utext="${goods.lable}"></textarea>
                    </div>
                </div>
                <!--                <div class="layui-form-item">-->
                <!--                    <label class="layui-form-label">性别</label>-->
                <!--                    <div class="layui-input-block">-->
                <!--                        <input type="radio" name="gender" value="1" title="男" >-->
                <!--                        <input type="radio" name="gender" value="0" title="女" checked>-->
                <!--                    </div>-->
                <!--                </div>-->
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="editGood">修改</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>

            </form>
        </div>
    </div>
</div>
<script th:inline="javascript">
    layui.use(['layer', 'form', 'laydate'], function () {
        var $ = layui.jquery;
        var form = layui.form;

        form.on('submit(editGood)', function (obj) {
            console.log(obj)
            //注意此处categorylevelone的传值
            //AJAX请求
            obj.field.gid=[[${goods.gid}]];
            $.post("editGood", obj.field, function (d) {
                console.log("d=" + d);
                if (d !== 1) {
                    layer.msg(d);
                } else {
                    //关闭弹出层
                    // layer.getFrameIndex(windowName) - 获取特定iframe层的索引
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                }
            }, "json");
            //取消form提交的默认submit行为
            return false;
        });
    })
</script>
</body>
</html>